/**
 * @class   wosh::Settings
 * @brief   The class collects multiple set (groups) of wosh::Property objects.
 *
 ****************************************************************************
 * @version $Id: Settings.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_Settings_H__
 #define __WOSH_Core_Settings_H__

 #include <woshDefs.h>
 #include <core/Object.h>
 #include <core/Mutex.h>
 #include <core/LogContext.h>
 #include <core/PropertiesProvider.h>
 #include <map>


namespace wosh {

 #define _Settings_NAME				"Settings"
 #define _Settings_KEY_ProgramName	"ProgramName"


class Settings : public Object
 {
	WOSH_SERIALIZABLE(wosh::Settings)
	WOSH_COMPARE_RTTI(wosh::Settings)

	typedef MapT<std::string,PropertiesProvider*> tStrPropertiesProviderMap;


	public:
		/**
		 * @brief  Empty constructor.
		 */
		Settings();
		/**
		 * @brief  Constructor with standard entry point initializer.
		 * @param argc [in] number of arguments, may be 0
		 * @param argv [in] array of c strings (arguments), size is argc
		 * @see    main()
		 */
		Settings( int argc, char **argv );
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		Settings( const Settings& m ) : Object(m) {
			*this = m;
		 }
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~Settings();


	public:
		WRESULT loadFromArgs( int argc, char **argv, bool overwrite = true );

		WRESULT loadConfigurationFile( const std::string& filename );

		/**
		 * @brief  load all configuration files into settings, not recursive
		 * @param [in] folder generic path (wosh or local)
		 * @param [in] pattern file pattern
		 * @return WRET_OK
		 */
		WRESULT loadConfigurationFolder( const std::string& folder, const std::string& pattern = ".conf" );


	public:
		WRESULT set( const std::string& key, const Variant& value, const std::string& group, bool never_overwrite = false );

	public:

		int getGroups(std::vector<std::string>& groups_name) const;

		PropertiesProvider* getSettingsOf( const std::string& group );

		Variant getValueOf( const std::string& group, const std::string& key );
		Variant getValueOf( const std::string& group, const std::string& key, const Variant& default_value = Variant::EmptyVariant );

		bool existsGroup( const std::string& group ) const;

		long countGroups() const							{ return this->groups.count(); }
		long countKeys() const;
		long countKeysOf( const std::string& group ) const;

	protected:
		PropertiesProvider* getSettingsOf_( const std::string& group );

		WRESULT injectLine( const std::string& dataLine, const std::string& group, long iLine = -1, bool never_overwrite = false );
		WRESULT parseLine( const std::string& dataLine, std::string& group, std::string& key, Variant& value );

/** @name Operators Overloading
 * @{
 ******************************************************************************/
	public:
		Settings& operator=(const Settings& m);
		bool operator==(const Settings &other) const;
		bool operator!=(const Settings &other) const	{ return !(*this == other);  }
//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:
		virtual std::string toString() const;
		virtual bool updateHash( DataHashContext* hash_context ) const;
//@}
	protected:
		mutable LogContext Log;
		PropertiesProvider genericGroup;			///< generic properties group (empty group-name)
		mutable tStrPropertiesProviderMap groups;	///< store groups and their properties


}; // class def

}; // namespace wosh

#endif //__WOSH_Core_Settings_H__
